/*
 * Virtual Earth Screensaver
 * Keith Kinnan
 * 5/12/2007
 * 9/30/2006
 * 
 * Portions of this code is based on the Visual Studio 2005 Starter Kit RSS Screen Saver:
 * http://msdn.microsoft.com/vstudio/express/visualcsharp/starterkit/default.aspx#rssss
 */

using System.Text.RegularExpressions;

namespace VirtualEarthScreensaver
{
    /// <remarks>
    /// Virtual Earth RSS Reader
    /// </remarks>
    public sealed class VERssReader
    {
        /// <summary>
        /// Transforms an existing RssItem into an RssItem with Virtual Earth relevant data
        /// </summary>
        /// <param name="rssItem">RSS Item</param>
        /// <returns></returns>
        public static RssItem Transform(RssItem rssItem)
        {
            return new RssItem(ExtractTitle(rssItem), ExtractDescription(rssItem), ExtractLink(rssItem), rssItem.Content, rssItem.Latitude, rssItem.Longitude);
        }

        /// <summary>
        /// Extracts a Virtual Earth permalink from an existing RssItem
        /// </summary>
        /// <param name="rssItem">RSS Item</param>
        /// <returns></returns>
        private static string ExtractLink(RssItem rssItem)
        {
            const string sLiveSearchMapsUrlRegEx = "http://(maps|local).live.com[^\"']*";
            const string sLiveSearchMapsUrlQuotedRegEx = "([\"'])" + sLiveSearchMapsUrlRegEx + "\\1";

            string linkStr = null;

            if (rssItem != null)
            {
                //Try getting a Virtual Earth URL from the RSS Link
                if (rssItem.Link != null)
                {
                    Match match = Regex.Match(rssItem.Link, sLiveSearchMapsUrlRegEx);
                    if (match.Success)
                    {
                        linkStr = FilterLink(rssItem.Link);
                    }
                }

                //Try getting a Virtual Earth URL from the RSS Content
                if (linkStr == null)
                {
                    if (rssItem.Content != null)
                    {
                        Match match = Regex.Match(rssItem.Content, sLiveSearchMapsUrlQuotedRegEx);
                        if (match.Success)
                        {
                            linkStr = FilterLink(match.ToString());
                        }
                    }
                }

                //Try getting a Virtual Earth URL from the RSS Description
                if (linkStr == null)
                {
                    if (rssItem.Description != null)
                    {
                        Match match = Regex.Match(rssItem.Description, sLiveSearchMapsUrlRegEx);
                        if (match.Success)
                        {
                            linkStr = FilterLink(match.ToString());
                        }
                    }
                } 
                
            }

            return linkStr;
        }

        /// <summary>
        /// Extracts a plain text Title from an existing rssItem
        /// </summary>
        /// <param name="rssItem">RSS Item</param>
        /// <returns></returns>
        private static string ExtractTitle(RssItem rssItem)
        {
            string title = string.Empty;
            if (rssItem != null)
            {
                title = FilterText(rssItem.Title);
            }
            return title;
        }

        /// <summary>
        /// Extracts a plain text Description from an existing rssItem
        /// </summary>
        /// <param name="rssItem"></param>
        /// <returns></returns>
        private static string ExtractDescription(RssItem rssItem)
        {
            string descr = string.Empty;
            if (rssItem != null)
            {
                descr = FilterText(rssItem.Description);
            }
            return descr;
        }

        /// <summary>
        /// Filter special characters out of Link
        /// </summary>
        /// <param name="linkStr">link string</param>
        /// <returns></returns>
        private static string FilterLink(string linkStr)
        {
            return linkStr.Trim("'\"".ToCharArray()).Replace("&amp;", "&");
        }

        /// <summary>
        /// Filter special characters out of text including HTML markup
        /// </summary>
        /// <param name="textStr">text string</param>
        /// <returns>filtered string</returns>
        private static string FilterText(string textStr)
        {
            Regex regex = new Regex("</?(.*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            return regex.Replace(textStr, string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\"", "\\\"");
        }

        /// <summary>
        /// private constructor
        /// </summary>
        private VERssReader() {}
    }
}
